'use strict';
module.exports = (app) => {
  // 商品表
  const { STRING, INTEGER, FLOAT, TEXT } = app.Sequelize;
  const Goods = app.model.define('s_goods', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    name: {
      type: STRING,
      comment: '商品名称',
      allowNull: false,
    },
    desc: {
      type: STRING,
      comment: '商品描述',
      allowNull: true,
    },
    shops_id: {
      type: INTEGER,
      comment: '商家id',
      allowNull: false,
    },
    project_id: {
      type: INTEGER,
      comment: '项目id',
      allowNull: false,
    },
    classify_id: {
      type: INTEGER,
      comment: '分类id',
      allowNull: false,
    },
    price: {
      type: FLOAT,
      comment: '价格',
      allowNull: false,
    },
    sale_price: {
      type: FLOAT,
      comment: '优惠价格',
    },
    photo_ids: {
      type: STRING,
      comment: '图片id组',
      allowNull: false,
    },
    stock_num: {
      type: INTEGER,
      comment: '库存数量',
      allowNull: false,
    },
    sold_num: {
      type: INTEGER,
      comment: '已售数量',
      defaultValue: 0,
    },
    search_num: {
      type: INTEGER,
      comment: '搜索次数',
      defaultValue: 0,
    },
    status: {
      type: STRING,
      comment: '状态，0停用,1可用,2删除',
      defaultValue: '1',
    },
    rich_text: {
      type: TEXT,
      comment: '商品详情',
    },
    classify_ids: {
      type: STRING,
      comment: '分类id组',
      allowNull: false,
    },
    attributeList: {
      type: STRING,
      comment: '规格列表',
      allowNull: true,
    },
  });
  Goods.associate = function () {
    // 一对多链接商品图片表
    app.model.Tables.Shop.Goods.hasMany(app.model.Tables.Shop.GoodsPhoto, {
      foreignKey: 'point_id',
    });
    // 多对一链接商家表
    app.model.Tables.Shop.Goods.belongsTo(app.model.Tables.Com.Admin, {
      foreignKey: 'shops_id',
    });
    // 关联分类表
    app.model.Tables.Shop.Goods.belongsTo(app.model.Tables.Shop.Classify, {
      foreignKey: 'classify_id',
    });
  };

  return Goods;
};
